package websimulator;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import readwrite.Connector;
import users.User;

/**
 * Servlet implementation class EditUser
 */
@WebServlet("/EditUser")
public class EditUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Connector con;    
    /**
     * @throws Exception 
     * @see HttpServlet#HttpServlet()
     */
    public EditUser() throws Exception {
        super();
        con = new Connector();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int userId =Integer.parseInt(request.getParameter("userId"));
		User user = null;
		
		try {
			user = con.getUser(userId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String answer = "<div id=\"Users\">";
			   answer += "<table> <tr> <td>Username: </td> <td><input type=\"Text\" name=\"user_edit_name\" id=\"user_edit_name\" value=\""+user.getUserName()+"\"></td></tr>"+
					   "<tr><td>Access Level: </td><td><select id=\"user_edit_access\" name=\"user_edit_access\"> ";
					   
		if(user.getAccessLevel() == 1)	   
		{
			   answer += "<option selected=\"selected\" value=\"1\">Adminitrator</option>" +
					     "<option value=\"2\">Normal User</option></td></tr>";
		}
		else
			answer += "<option value=\"1\">Adminitrator</option>" +
				     "<option selected=\"selected\" value=\"2\">Normal User</option></td></tr>";
		answer += "<input type=\"hidden\" name=\"user_edit_Id\" id=\"user_edit_Id\" value=\""+userId+"\">";
		answer += "<tr><td> <input type=\"button\" id=\"submitChanges\" value=\"OK\" onclick= \"edituser()\" style=\"margin-top: 10px; font-size:16px; width: 100px; height: 30px;\"> </td></tr>";
		//answer += "<tr><td> <input type=\"button\" id=\"cancel\" value=\"cancel\" onclick= \"location.href='Menu.jsp'\"> </td></tr>";
		answer += "</table></div>";
		
		response.setContentType("text/plain");
		response.getWriter().write(answer);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String user_slett = request.getParameter("user_slett");
		
		if (user_slett != null){
			int userID = Integer.parseInt(request.getParameter("userId"));
			try {
				con.RemoveUser(userID);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			int userID = Integer.parseInt(request.getParameter("user_edit_Id"));
			int AccessRights = Integer.parseInt(request.getParameter("user_edit_access"));
			String username = request.getParameter("user_edit_name");
			
			try 
			{
				con.ChangeUser(username, AccessRights, userID);
				
			} 
			catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		response.sendRedirect("UserAccounts.jsp");
	}

}
